package com.icare.common.service.exception;

import com.alibaba.fastjson.JSON;
import com.icare.common.service.log.LogAspect;
import com.icare.shop.common.base.ResponseResult;
import com.icare.shop.common.constant.GlobalEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


/**
 * 全局异常处理器
 *
 * @author TianHaibo
 * @create 2017-12-06 17:50
 **/

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
    private final static String ERROR_MESSAGE = "对不起，系统开小差啦...";

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult<String> defaultErrorHandler(HttpServletRequest r, Exception e) throws Exception{
        e.printStackTrace();
        ResponseResult<String> result = ResponseResult.faild(GlobalEnum.ERROR.getCode()
                , ERROR_MESSAGE, "详情：" + e.toString());
        log.error("{\"出现异常error\":{}}", JSON.toJSONString(result));
        return result;
    }

}
